package com.lz.autotest.common.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;
import org.dom4j.xpath.DefaultXPath;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.slf4j.LoggerFactory.getILoggerFactory;


public class CommonUtils {


    public static Logger getLogger(Class<?> clazz) {
        ILoggerFactory iLoggerFactory = getILoggerFactory();
        return iLoggerFactory.getLogger(clazz.getName());
    }

    public static List<String> getResultByNodeNameForNameSpace(String xml, String nodeName, String nameSpaceName, String nameSpaceUrl) throws DocumentException {
        Document doc = DocumentHelper.parseText(xml);
        DefaultXPath xPath = new DefaultXPath("//" + nodeName);
        xPath.setNamespaceURIs(Collections.singletonMap(nameSpaceName, nameSpaceUrl));
        List<Node> nodes = xPath.selectNodes(doc);
        List<String> list = new ArrayList<>();
        for (Node node:nodes){
            list.add(node.getText());
        }
        return list;
    }

}
